import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.SocketAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.game79.musou.util.CampaignId;
import com.game79.musou.util.TechId;
import com.game79.musou.util.TrainingStyle;
import com.game79.musou.webapp.action.ArenaDataAction;
import com.game79.musou.webapp.action.BattlefieldAction;
import com.game79.musou.webapp.action.CampaignAction;
import com.game79.musou.webapp.action.HeroAction;
import com.game79.musou.webapp.action.HeroBiographyAction;
import com.game79.musou.webapp.action.ItemAction;
import com.game79.musou.webapp.action.ManorAction;
import com.game79.musou.webapp.action.OverComeBowlderAction;
import com.game79.musou.webapp.action.PlayerAction;
import com.game79.musou.webapp.action.PrivilegeCardAction;
import com.game79.musou.webapp.action.TechConfigAction;
import com.game79.musou.webapp.action.VipServiceAction;

import flex.messaging.io.amf.ASObject;

public class Test {
	
	public static void main(String args[]) throws InterruptedException{
		AMFConnectionEntry entry = new AMFConnectionEntry();
		Proxy proxy = null;
		//SocketAddress sa = new InetSocketAddress("162.105.30.241", 8443);
		//SocketAddress sa = new InetSocketAddress("127.0.0.1", 8888);
		//proxy = new Proxy(Type.HTTP, sa);
		entry.init(proxy);
		entry.connect(9, "C820561797CA66AFDF03A40F47DDF411");
		//entry.connect(1, "84EDA26B70E39F8653E7DF656D1CDB99");
		//System.out.println(entry.call("itemAction.autoExploreTreasureMap", new ItemAction().autoExploreTreasureMap(-2, 3000, 1, 0)));
		//System.out.println(entry.call("overComeBowlderAction.wipeOutBowlder", new OverComeBowlderAction().wipeOutBowlder()));
		//entry.call("playerAction.levelup", new PlayerAction().levelup());
		//System.out.println(entry.call("vipServiceAction.vipBuyBowlder", new VipServiceAction().vipBuyGem("IC_BOLWDER_ORIGINAL_5", 3)));
		//System.out.println(entry.call("itemAction.equipItem", new ItemAction().equipItem(1440258262540056L, 1440142239647056L)));//1.440088435187009
		//System.out.println(entry.call("itemAction.unequipItem", new ItemAction().unequipItem(1440090012197056L)));
		//System.out.println(entry.call("privilegeCardAction.receivePropValue", new PrivilegeCardAction().receivePropValue(2)));
		//researchTech(entry, TechId.BR, 10000, 22);
		//arena(entry);
		//casCampaign(entry, 1438154119229000L, 1);
		/*for(int i=0;i<300;i++){
			//System.out.println(entry.call("itemAction.exchangeWine", new ItemAction().exchangeWine(5,2)));
			System.out.println(entry.call("itemAction.autoExploreTreasureMap", new ItemAction().autoExploreTreasureMap(2, 5000000, 1, 0)));//1329709126697000
			entry.call("itemAction.extractFromAllFateStar", new ItemAction().extractFromAllFateStar(5));
		}*/
		/*int num = 200;
		System.out.println(entry.call("vipServiceAction.recordYanXiHerosNum", new VipServiceAction().recordYanXiHerosNum(num)));
		while(num>=1){
			System.out.println(entry.call("vipServiceAction.vipYanXiHeros", new VipServiceAction().vipYanXiHeros(num, 1, 1432945031040009L, false)));//zhaoyun:1432945031040009
			num--;
		}*/
		for(int i=0;i<4;i++)liezhuan(entry, 3, i);
		//bowlderCampaign(entry, 15);
		//entry.call("manorAction.takeRansackPrize", new ManorAction());
		/*List<Double> table = (List<Double>) ((List<Map<String, Object>>)entry.call("playerAction.initData", new PlayerAction()).get("objects")).get(1).get("heroLevelupExpTable");
		double[] exps = new double[170];
		double[] zhuan = new double[13];
		double[] lei = new double[13];
		for(int i=0;i<170;i++){
			exps[i] = table.get(i+1);
			int z = i/10 -4;
			if(z<0)z=0;
			if(z>0&&i%10==0)zhuan[z]=zhuan[z-1];
			zhuan[z]+=exps[i];
		}	
		for(int i=0;i<13;i++){
			for(int j=0;j<=i;j++){
				lei[i]+=zhuan[j];
			}
			System.out.println((long)lei[i]*4);
		}*/
//		System.out.println((long)(lei[9]+zhuan[9])*4);
		//System.out.println(entry.call("manorAction.ransackCrops",new ManorAction().ransackCrops(1335061611206016L)));//1362410037169016
		//System.out.println(entry.call("heroAction.recruitActivityHero",new HeroAction().recruitActivityHero("HERO_JIA_XU")));
		//System.out.println(entry.call("tradeRouteAction.requestBuildTradeRoute", new TradeRouteAction().requestBuildTradeRoute("鑰堕瓟")));
		//System.out.println(entry.call("vipServiceAction.safeSellResource", new VipServiceAction().safeSellResource(1280135769038001L, 1348206072984016L)));;
		//String formation = "<formation><grid x=\"0\" y=\"1\" hLabel=\"HERO_GUO_JIA\" wId=\"1381076033289016\"/></formation>";
		//Object o = entry.call("playerAction.saveFormation", new PlayerAction().saveFormation(formation));
		//System.out.println(o);//1366099498166000,1366099660171000,1366099822176000,1366101754183000,1366101846188000,1366102493194000,1366103238199000,1366103360204000,
		//Map root = entry.call("manorAction.viewManor", new ManorAction().viewManor(1362410037195016L));
		//System.out.println(((List<HashMap>)(root.get("objects"))).get(0).get("sumTodayRansackTimes"));
		//System.out.println(entry.call("playerAction.selectCountry", new PlayerAction().selectCountry(2)));
		//System.out.println(entry.call("resourceFieldAction.giveUpResourceField", new ResourceFieldAction().giveUpResourceField()));
		//entry.call("battlefieldAction.leave", new BattlefieldAction());//1338445623334000;1282652687073000;1282625761172000
		//System.out.println(entry.call("resourceFieldAction.occupyResourceField", new ResourceFieldAction().occupyResourceField(1282652687073000L, 3, 2)));
		//System.out.println(entry.call("resourceFieldAction.occupyResourceField", new ResourceFieldAction().occupyResourceField(1338444854166000L, 5, 3)));
		//System.out.println(entry.call("resourceFieldAction.occupyResourceField", new ResourceFieldAction().occupyResourceField(1366168866003000L, 1, 2)));
		entry.destroy();
					//amfConnection.call("campaignAction.casCampaign", new CampaignAction().casCampaign(1323845768023000L));
					//amfConnection.call("battlefieldAction.leave", new BattlefieldAction());
					//System.out.println(amfConnection.call("vipServiceAction.eraseMiniCD", new VipServiceAction().eraseMiniCD(-1, "ENHANCE_CD")));
					//System.out.println(amfConnection.call("vipServiceAction.vipBuyGem", new VipServiceAction().vipBuyGem("IC_MATERIAL_WU_CAN_SI", (Integer.MAX_VALUE/900))));
					//System.out.println(amfConnection.call("itemAction.sealItem", new ItemAction().sealItem(1346924304464032L)));
					//System.out.println(amfConnection.call("itemAction.compoundItem", new ItemAction().compoundItem(1331131188273000L, Integer.MAX_VALUE/10)));
					//System.out.println(amfConnection.call("itemAction.equipItem", new ItemAction().equipItem(1346612654680022L, 1340108337787022L)));
					//System.out.println(amfConnection.call("itemAction.autoExploreTreasureMap", new ItemAction().autoExploreTreasureMap(4, 25013400, 3, 1)));
					//System.out.println(amfConnection.call("battlefieldAction.jump", new BattlefieldAction().jump(1343324674381L, "BF_3", 3)));
					
					//System.out.println(amfConnection.call("manorAction.sellFood", new ManorAction().sellFood(130)));
					//amfConnection.call("allianceAction.inviteJoin", new AllianceAction().inviteJoin("angus1006", "f"));
					//Map<String, Object> r = getResult("manorAction.viewManor", new ManorAction().viewManor(1333137223427001L));
					//System.out.println(r.get("trading"));
					//amfConnection.call("techConfigAction.researchTech", new TechConfigAction().researchTech(1294389980354000l, 12397));//3261,3598,5264,12397,13637,15000
					//if(acceptMissionByMissionSetId(1321872322020000l))System.out.println("success");
					//System.out.println(getResult("officerAction.recruitOfficer", new OfficerAction().recruitOfficer("杞︽嘲鐒�)));
	}
	
	private static void bowlderCampaign(AMFConnectionEntry entry, int s) {
		long[] ids = new long[]{1438154119229000L, 1438159465472000L, 1438159798486000L, 1438159933497000L, 1438160045508000L, 1438160232519000L, 1438160361530000L, 1438160470541000L, 1438160589552000L, 1438160688563000L, 1438160807574000L, 1438160930585000L, 1438161115596000L, 1438161356607000L, 1438161447618000L, 1438161534629000L, 1438161642640000L, 1438161785651000L, 1438162065662000L, 1438162214673000L, 1438162369684000L, 1438162539695000L, 1438162719706000L, 1438162905717000L, 1438163093734000L, 1438163353750000L, 1438163479761000L};
		entry.call("overComeBowlderAction.casOverComeBowlderCampaign", new OverComeBowlderAction().casOverComeBowlderCampaign(ids[s]));
		entry.call("battlefieldAction.leave", new BattlefieldAction());
	}

	static void researchTech(AMFConnectionEntry entry, long techId, int points, int researchLevel){
		int[] us = {15000,13637,12397,11279,10274,9317,8475,7732,7010,6383,5792,5264,4793,0,3958,3598,3261,0,2698,0,0,2030,1848};
		int unit = us[researchLevel];
		//int u = (int) Math.ceil(unit);
		System.out.println(unit);
		for(int i=0; i<points; i++){
			entry.call("techConfigAction.researchTech", new TechConfigAction().researchTech(techId, unit));
			System.out.println(i);
		}
	}
	
	static void casCampaign(AMFConnectionEntry entry, long campaignId, int times){
		for(int i=0; i<times; i++){
			entry.call("campaignAction.casCampaign", new CampaignAction().casCampaign(campaignId));
			entry.call("battlefieldAction.leave", new BattlefieldAction());
			System.out.println(i);
		}
	}
	
	@SuppressWarnings("unchecked")
	static void arena(AMFConnectionEntry entry) throws InterruptedException{
		Map root = entry.call("arenaDataAction.viewArenaData", new ArenaDataAction().viewArenaData());
	
		List<Map<String, Object>> results = (List<Map<String, Object>>) root.get("objects");
		List<Map<String, Object>> opponentList = (List<Map<String, Object>>) results.get(1).get("opponentList");
		for(int i=0; i<8; i++){
			if((boolean)opponentList.get(i).get("defeated")){
				System.out.println(opponentList.get(i).get("name"));
			}else{
				entry.call("arenaDataAction.challengeOpponent", new ArenaDataAction().challengeOpponent(new Double((double)opponentList.get(i).get("id")).longValue()));
				entry.call("battlefieldAction.leave", new BattlefieldAction());
				System.out.println(i);
			}
		}
	}
	
	static void trainHero(AMFConnectionEntry entry, long heroId, int style, int times, int a, int b, int c, int heroLevel){
		for(int i=0; i<times; i++){
			Map<String, Object> root = entry.call("heroAction.trainHero", new HeroAction().trainHero(heroId));
			@SuppressWarnings("unchecked")
			List<Map<String, Object>> results = (List<Map<String, Object>>) root.get("objects");
			Map<String, Object> hero = results.get(1);
			//System.out.println(hero);
			double a1=(double)hero.get("constitution")-a, a2=(double)hero.get("constitutionTemp")-a;
			double b1=(double)hero.get("intelligence")-b, b2=(double)hero.get("intelligenceTemp")-b;
			double c1=(double)hero.get("dexterity")-c, c2=(double)hero.get("dexterityTemp")-c;
			boolean change = false;
			//System.out.println(a1+","+b1+","+c1+"  "+a2+","+b2+","+c2);
			if(style==TrainingStyle.ONE_CONSTITUTION){
				if(a1==heroLevel)return;
				if(a2>a1)change = true;
			}else if(style==TrainingStyle.ONE_INTELLIGENCE){
				if(b1==heroLevel)return;
				if(b2>b1)change = true;
			}else if(style==TrainingStyle.ONE_DEXTERITY){
				if(c1==heroLevel)return;
				if(c2>c1)change = true;
			}else if(style==TrainingStyle.TWO_CI){
				if(a1==heroLevel&&b1==heroLevel)return;
				if(checkTwo(a1, a2, b1, b2))change = true;
			}else if(style==TrainingStyle.TWO_CD){
				if(a1==heroLevel&&c1==heroLevel)return;
				if(checkTwo(a1, a2, c1, c2))change = true;
			}else if(style==TrainingStyle.TWO_ID){
				if(c1==heroLevel&&b1==heroLevel)return;
				if(checkTwo(b1, b2, c1, c2))change = true;
			}else if(style==TrainingStyle.THREE){
				if(a1==heroLevel&&b1==heroLevel&&c1==heroLevel)return;
				if((a2+b2+c2-a1-b1-c1)>0)change = true;
			}else return;
			if(change){
				entry.call("heroAction.confirmHeroTraining", new HeroAction().confirmHeroTraining(heroId));
				System.out.println("  "+a2+","+b2+","+c2);
			}
		}	
	}
	static boolean checkTwo(double a1, double a2, double b1, double b2){
		double x = a2 + b2 - a1 - b1;
		double y1 = Math.abs(a1-b1);
		double y2 = Math.abs(a2-b2);
		double y = (y2-y1)*Math.sqrt(y1+1)/6;
		if(x>y){
			//System.out.println(x+","+y);
			return true;
		}else return false;
	}
	static void liezhuan(AMFConnectionEntry entry, int type, int level) throws InterruptedException{
		long[][] map = {
				{1383200936908000L,1383200983913000L,1383201015918000L,1383201039923000L}
				,{1383191858752000L,1383192068757000L,1383192167762000L,1383192203767000L}
				,{1404729577019000L,1404730229042000L,1404730548053000L,1404730828064000L}
				,{1419411610023000L,1419411787033000L,1419411887038000L,1419412103068000L}
		};
		boolean vip = true;
		//if(level>=1)vip = true;
		entry.call("heroBiographyAction.casBiographyCampaign", new HeroBiographyAction().casBiographyCampaign(map[type][level], type, vip));
		entry.call("battlefieldAction.leave", new BattlefieldAction());
	}
}